<template>
  <div class="todo-main">
    <el-card v-if="todoList.length === 0" class="empty-tip"> 暂无待办事项 </el-card>
    <el-card v-else>
      <div v-for="(item, index) in todoList" :key="item.id" class="todo-item">
        <div class="todo-content">
          <el-checkbox v-model="item.completed" @change="() => handleToggle(index)">
            <span :class="{ completed: item.completed }">{{ item.title }}</span>
          </el-checkbox>

          <div class="todo-info">
            <el-tag :type="getPriorityType(item.priority)" size="small" class="priority-tag">
              {{ getPriorityLabel(item.priority) }}
            </el-tag>

            <el-tag
              v-if="item.dueDate"
              :type="isOverdue(item.dueDate) ? 'danger' : 'info'"
              size="small"
              class="due-date-tag"
            >
              {{ item.dueDate }}
            </el-tag>

            <div class="tags-container">
              <el-tag v-for="tag in item.tags" :key="tag" size="small" class="tag-item">
                {{ tag }}
              </el-tag>
            </div>
          </div>
        </div>

        <div class="todo-actions">
          <el-button type="primary" size="small" @click="handleEdit(index)" class="edit-btn">
            编辑
          </el-button>
          <el-button type="danger" size="small" @click="handleDelete(index)" class="delete-btn">
            删除
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" title="编辑待办事项" width="500px">
      <el-form :model="editForm" label-width="80px">
        <el-form-item label="待办事项">
          <el-input v-model="editForm.title" />
        </el-form-item>

        <el-form-item label="截止日期">
          <el-date-picker
            v-model="editForm.dueDate"
            type="datetime"
            placeholder="选择截止日期"
            format="YYYY-MM-DD HH:mm"
            value-format="YYYY-MM-DD HH:mm"
          />
        </el-form-item>

        <el-form-item label="优先级">
          <el-select v-model="editForm.priority" placeholder="选择优先级">
            <el-option label="高" value="high" />
            <el-option label="中" value="medium" />
            <el-option label="低" value="low" />
          </el-select>
        </el-form-item>

        <el-form-item label="分类">
          <el-select
            v-model="editForm.tags"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="选择或创建分类标签"
          >
            <el-option v-for="tag in availableTags" :key="tag" :label="tag" :value="tag" />
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmEdit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({
  todoList: {
    type: Array,
    required: true,
  },
})

const emit = defineEmits(['delete-todo', 'toggle-todo', 'edit-todo'])

const editDialogVisible = ref(false)
const editIndex = ref(-1)
const editForm = ref({
  title: '',
  dueDate: '',
  priority: 'medium',
  tags: [],
})

const availableTags = ref(['工作', '学习', '生活', '娱乐'])

const handleDelete = (index) => {
  emit('delete-todo', index)
}

const handleToggle = (index) => {
  emit('toggle-todo', index)
}

const handleEdit = (index) => {
  editIndex.value = index
  editForm.value = { ...props.todoList[index] }
  editDialogVisible.value = true
}

const confirmEdit = () => {
  emit('edit-todo', {
    index: editIndex.value,
    todo: { ...editForm.value },
  })
  editDialogVisible.value = false
}

const getPriorityType = (priority) => {
  switch (priority) {
    case 'high':
      return 'danger'
    case 'medium':
      return 'warning'
    case 'low':
      return 'info'
    default:
      return 'info'
  }
}

const getPriorityLabel = (priority) => {
  switch (priority) {
    case 'high':
      return '高优先级'
    case 'medium':
      return '中优先级'
    case 'low':
      return '低优先级'
    default:
      return '未设置'
  }
}

const isOverdue = (dueDate) => {
  if (!dueDate) return false
  return new Date(dueDate) < new Date()
}
</script>

<style scoped>
.todo-main {
  margin: 20px 0;
}

.empty-tip {
  text-align: center;
  color: #909399;
  padding: 20px;
}

.todo-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #ebeef5;
}

.todo-item:last-child {
  border-bottom: none;
}

.todo-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.todo-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: 24px;
}

.todo-actions {
  display: flex;
  gap: 8px;
}

.completed {
  text-decoration: line-through;
  color: #909399;
}

.priority-tag {
  min-width: 80px;
  text-align: center;
}

.due-date-tag {
  min-width: 120px;
  text-align: center;
}

.tags-container {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.tag-item {
  margin-right: 4px;
}
</style>
